package com.etrans.web.gps.business.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.base.domain.VehicleExpand;
import com.etrans.web.gps.business.system.domain.PermissionMenu;
import com.etrans.web.gps.business.system.domain.SysUser;
import com.etrans.web.gps.business.system.service.MenuService;
import com.etrans.web.gps.common.Constant;

/**
 * 登录菜单权限管理
 * @author jw.chen
 *
 */
@Controller
@RequestMapping("/system/menu/*")
public class MenuController extends BaseController{
	
	@Autowired
	private MenuService menuService;
	
	/**
	 * 加载用户权限菜单
	 * @param req HttpServletRequest
	 * @param res  HttpServletResponse
	 * @param model Model 
	 * @return String 返回菜单页面
	 */
	@RequestMapping(value="loadMenu.html")
	@ResponseBody
	public  Map<String, Object> loadMenu(HttpServletRequest req,HttpServletResponse res, Model model){
		SysUser user = (SysUser)getSession().getAttribute(Constant.LOGIN_SESSION_NAME);
		PermissionMenu pMenu = (PermissionMenu)getSession().getAttribute(Constant.USER_OP_MENU);
		if(null == pMenu){
			pMenu = menuService.getPermissionMenuByUserId(user);
			getSession().setAttribute(Constant.USER_OP_MENU, pMenu);
		}
		Map<String, Object> map=new HashMap<>();
		map.put("menuList", pMenu);		
		return map;
	}
	
	
	@RequestMapping(value="loadResportsetMenu.html")
	@ResponseBody
	public ModelAndView loadResportsetMenu(){
		ModelAndView modelAndView = new ModelAndView();
		List<PermissionMenu> list = menuService.loadResportsetMenu();
		modelAndView.addObject("menuList", list);
		modelAndView.setViewName("views/business/reportset/conditionSetting");
		return modelAndView;
	}
	
	/**
	 * 跳转到框架页面
	 * @param menuId
	 * @return
	 */
	@RequestMapping(value = "toContentManager.html")
	@ResponseBody
	public ModelAndView toContentManager(@RequestParam String menuId,String licence){
		ModelAndView mav = new ModelAndView();
		mav.addObject("licence", licence);
		mav.addObject("menuId", menuId);
		mav.addObject("vehicle", new VehicleExpand());
		mav.setViewName("views/common/contentManager");
		return mav;	
	}
	
	/**
	 * 加载子菜单
	 * @param response
	 * @param request
	 */
	@RequestMapping(value="loadSubMenu.html")
	public void loadSubMenu(HttpServletResponse response,HttpServletRequest request){
		SysUser user = this.getSysUser();
		HashMap<String,Object> params = this.getReqParameters();
		
		if(!user.isSuper_admin()){
			params.put("userId", user.getId());
		}
		List<HashMap<String,Object>> list = menuService.loadThreeMenu(params);
		this.ajaxResultJson(response, list);
	}
	
}
